/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.io.File;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.framework.base.utils.Constants;
import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.momarketing.common.QRCodeUtils;
import com.boxin.ims.modules.momarketing.dao.QRCodeDao;
import com.boxin.ims.modules.momarketing.entity.QRCode;
import com.boxin.ims.modules.wechat.dao.SeckillGainerDao;
import com.boxin.ims.modules.wechat.entity.SeckillGainer;
import com.boxin.ims.modules.wechat.entity.UserDefine;
import com.thinkgem.jeesite.common.persistence.BaseEntity;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.common.utils.FileUtils;

/**
 * 微信秒杀Service
 * @author Jakemanse
 * @version 2013-09-13
 */
@Component
@Transactional(readOnly = true)
public class SeckillGainerService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(SeckillGainerService.class);
	
	@Autowired
	private SeckillGainerDao seckillGainerDao;
	
	@Autowired
	private QRCodeDao QRCodeDao;
	
	
	@Autowired
	private UserDefineService userDefineService;
	
	public SeckillGainer get(Long id) {
		return seckillGainerDao.findOne(id);
	}
	
	public List<SeckillGainer> find(SeckillGainer seckillGainer){
		DetachedCriteria dc = seckillGainerDao.createDetachedCriteria();
		dc.createAlias("member", "member");
		dc.createAlias("seckill", "seckill");
		
		
		//所属卖家
		if(seckillGainer.getSeckill()!=null && seckillGainer.getSeckill().getUser() != null){
			dc.add(Restrictions.eq("seckill.user.id", seckillGainer.getSeckill().getUser().getId() ));
		}
		
		//所属会员
		if(seckillGainer.getMember() != null && seckillGainer.getMember().getId()!= null){
			dc.add(Restrictions.eq("member.id", seckillGainer.getMember().getId()));
		}
		
		
		//手机号
		if(seckillGainer.getMember() != null && !com.thinkgem.jeesite.common.utils.StringUtils.isEmpty(seckillGainer.getMember().getMobile())){
			dc.add(Restrictions.like("member.mobile", "%"+seckillGainer.getMember().getMobile()+"%"));
		}
		
		if (StringUtils.isNotEmpty(seckillGainer.getName())){
			dc.add(Restrictions.like("name", "%"+seckillGainer.getName()+"%"));
		}
		
		
		
		if (StringUtils.isNotEmpty(seckillGainer.getStatus())){
			dc.add(Restrictions.eq("status",  seckillGainer.getStatus() ));
		}
		
		
		if (StringUtils.isNotEmpty(seckillGainer.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+seckillGainer.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", SeckillGainer.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		
		return seckillGainerDao.find(dc);
		
	}
	
	public Page<SeckillGainer> find(Page<SeckillGainer> page, SeckillGainer seckillGainer) {
		DetachedCriteria dc = seckillGainerDao.createDetachedCriteria();
		dc.createAlias("member", "member");
		dc.createAlias("seckill", "seckill");
		
		
		//所属卖家
		if(seckillGainer.getUser()!=null && seckillGainer.getUser().getId() >0){
			dc.add(Restrictions.eq("user.id", seckillGainer.getUser().getId() ));
		}
		
		//手机号
		if(seckillGainer.getMember() != null && !com.thinkgem.jeesite.common.utils.StringUtils.isEmpty(seckillGainer.getMember().getMobile())){
			dc.add(Restrictions.like("member.mobile", "%"+seckillGainer.getMember().getMobile()+"%"));
		}
		
		if (StringUtils.isNotEmpty(seckillGainer.getName())){
			dc.add(Restrictions.like("name", "%"+seckillGainer.getName()+"%"));
		}
		
		if (StringUtils.isNotEmpty(seckillGainer.getStatus())){
			dc.add(Restrictions.eq("status",  seckillGainer.getStatus() ));
		}
		if (StringUtils.isNotEmpty(seckillGainer.getType())){
			if("1".equals(seckillGainer.getType())){
				dc.add(Restrictions.eq("type",  seckillGainer.getType() ));
			}
		}
		
		
		if(seckillGainer.getCreateTime() !=null){
			String timeField = "createTime";
			if (StringUtils.isNotEmpty(seckillGainer.getStatus()) && BaseEntity.STATUS_CLEAR.equals(seckillGainer.getStatus())){
				timeField = "updateTime";
			}
			dc.add(Restrictions.ge(timeField,seckillGainer.getCreateTime() ));
		}
		
		if(seckillGainer.getEndTime() !=null){
			String timeField = "createTime";
			if (StringUtils.isNotEmpty(seckillGainer.getStatus()) && BaseEntity.STATUS_CLEAR.equals(seckillGainer.getStatus())){
				timeField = "updateTime";
			}
			dc.add(Restrictions.le(timeField,seckillGainer.getEndTime()));
		}
		
		
		if (StringUtils.isNotEmpty(seckillGainer.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+seckillGainer.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", SeckillGainer.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return seckillGainerDao.find(page, dc);
	}
	

	@Transactional(readOnly = false)
	public void save(SeckillGainer seckillGainer) {
		
		int width = QRCodeUtils.WIDTH;
		int height =QRCodeUtils.HEIGHT;
		//生成二维码
		QRCode qrcode = new QRCode();
		String content = QRCodeUtils.generateSeckillSN(seckillGainer.getUser().getId());
		qrcode.setContent(content);
		String path = Constants.getSeckillQRCodeResourceSavePath();
		path += DateUtils.getTodayString()+File.separator+content;
		FileUtils.createDirectory(path);
		QRCodeUtils.generateQRCodePNG(content, path, width,height);
		qrcode.setFileName(content+".png");
		qrcode.setPngPath(path+".png");
//		
//
//		// 生成二维码
//		QRCode qrCode = new QRCode();
//		String qrPath = Constants.getSeckillQRCodeResourceSavePath();
//		qrCode.setContent(QRCodeUtils.generateSeckillSN(seckillGainer.getSeckill().getUser().getId()));
//		qrCode.setJpegPath(qrPath +qrCode.getContent());
//		File file = new File(qrCode.getJpegPath()+".jpeg");
//		qrCode.setFileName(file.getName());
//		//生成二维码
//		QRCodeUtils.generateQRCodeJPEG(qrCode.getContent(),
//				qrCode.getJpegPath(), QRCodeUtils.WIDTH, QRCodeUtils.HEIGHT);
//		
//		QRCodeUtils.generateQRCodePNG(content, path, width, height);
		
		qrcode.setName(seckillGainer.getName());
		qrcode.setUser(seckillGainer.getUser());
		qrcode.setMember(seckillGainer.getMember());
		qrcode.setType(QRCode.TYPE_SECKILL);
		
		QRCode savedqrCode = QRCodeDao.save(qrcode);
		seckillGainer.setNumber(qrcode.getContent());
		seckillGainer.setQrCode(savedqrCode);
		
		seckillGainerDao.save(seckillGainer);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		seckillGainerDao.deleteById(id);
	}
	
	@Transactional(readOnly = false)
	public void updateStatusById(Long id,String status) {
		seckillGainerDao.updateStatusById(id,status);
	}
	
}
